<?php
class Providerguarantee extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('provider_guarantee_model', 'provider_guarantee');
    }

    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $getData['uid'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('providerguarantee/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('providerguarantee/grid');
        $config['total_rows'] = $this->provider_guarantee->findByCount($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['list'] = $this->provider_guarantee->findPageList($num, $page_num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('providerguarantee/grid', $data);
    }

    public function add()
    {
        $this->load->view('providerguarantee/add');
    }

    public function addPost()
    {
        $postData = $this->input->post();
        $postData['uid'] = $this->uid;
        $postData['created_at'] = date('Y-m-d H:i:s');
        $status = $this->provider_guarantee->insert($postData);
        if ($status) {
            $this->success('providerguarantee/grid', '', '保存成功！');
        } else {
            $this->error('providerguarantee/grid', '', '保存失败！');
        }
    }

    public function edit($id)
    {
        $res = $this->provider_guarantee->findOne(['id' => $id]);
        $data['res'] = $res->row();
        $this->load->view('providerguarantee/edit', $data);
    }

    public function editPost()
    {
        $id = $this->input->post('id');
        $postData = $this->input->post();
        unset($postData['id']);
        $status = $this->provider_guarantee->update($postData, ['id' => $id]);
        if ($status) {
            $this->success('providerguarantee/grid', '', '保存成功！');
        } else {
            $this->error('providerguarantee/grid', '', '保存失败！');
        }
    }
}